<?php
declare (strict_types = 1);

namespace app\middleware;
use app\business\RedisCache;
use app\libs\Predis;
use think\Request;

class Check
{
    protected $redis = null;
    protected $request = null;


    public function __construct()
    {
        $this->redis = new Predis();
    }
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        
        $role = 'visitor';
        $controller = request()->controller();
        $action = request()->action();
        
        //获取token 并判断是否过期
        $token = request()->header('Authorization');
        $user_id=0;
        if(!empty($token)){
            $role = "user";
            $result=checkToken($token);
            if($result['code']!=1){
                return json([
                    'status' => -2,
                    'msg' => $result['msg'],
                    'data' => []
                ]); 
            }else{
                // //p(array($result['data']->uid)[0]);
                // //p(['user_id'=> array($result['data']->uid)[0],'token_password' =>'']);
                // Request::instance()->dispatch(['user_no'=>'345353','token_password' =>'aaa']);
                // $user_id=array($result['data']->uid)[0];
                // p($user_id);
            }
        }
       
         $resource = $controller . "::" . strtolower($action);
         
         if ($this->allow($role, $resource)) {
            return $next(request());
         }else{
            return json([
                'status' => -2,
                'msg' => '暂无法访问',
                'data' => []
            ]); 
         }
        
    }
    public function allow($role = "visitor", $resource)
    {
        $acl = array(
            'visitor' => array(
                'Index::index',
                'Permiss::getmenu', 
                 //用户
                'User::login',
                'User::register',
                'User::forgetpwd',            
                'Mobile::sendcode',

                //企业
                'Company::getlist',
            	'Company::companydetail',
            	'Company::getcompanytotal',
            		
                //园区
                'Park::getlist',
            	'Park::getparktotal',
            	
            	//政策
            	'Policy::getpolicytypelist',
            	'Policy::getpolicylist',
            	'Policy::getpolicydetail',
            	'Policy::getpolicytotal',
            		
            	//智库
            	'Tank::gettanklist',
            	'Tank::gettankdetail',
            	'Tank::gettanktotal',
            		
            	//投融
            	'Invest::getinvesteventlist',
            	'Invest::getinvestorganlist',
            		
                //标签
            	'Label::getchaindetailbyid',
                'Label::getcityinfo',
                'Label::getindustryinfo',
                'Label::getuserorplatformchain',
            	'Label::getcompanylabelinfo',
            	
                 //地图
            	'Map::compayorparknumonmap',//省市区对应下面的企业或者园区的数量统计
            	'Map::getcompanyheat',//企业热力图
            	
            	//新闻
            	'News::getnewslist',
            	'News::getnewsdetail',
            		
            	//舆情
            	'Opinions::getopinionstypelist',
            	'Opinions::getopinionslist',
            	'Opinions::getopinionsdetail',
            	
            	//视频
            	'Videos::getvideoslist',
            	'Videos::getvideosdetail',
            		
            	//本地招商项目
            	'Localprojects::getlocalprojectslist',
            	'Localprojects::getlocalprojectsdetail',
            	'Localprojects::getlocalprojectstotal',
            		
            		
            	//投资项目
            	'Investment::getinvestmentlist',
            	'Investment::getinvestmenttotal',
            		
            	//尽调报告
            	'Applycomreport::applycomreportadd',
            		
            	//产业报告
            	'Report::getreportlist',
            		
            	//平台
            	'Platform::getplatforminfo',
            		
            	//open
            	'Open::getmapjsonbycode',
            	'Open::uploadfife',
            	
            	//资源要素
            	'Resource::getinvestcost',
            	'Resource::getmap',
            ),
            'user' => array(
                'Index::index',

                 //权限
                'Permiss::getmenu', 
                'Permiss::getrolelist',
                'Permiss::getroledetail',                		
                'Permiss::roleedit',
                'Permiss::roledel',
                'Permiss::updaterolestatus',
                'Permiss::getadminlist',
                'Permiss::getadmindetail',
                'Permiss::adminedit',
                'Permiss::admindel' ,
                'Permiss::updateadminstatus',   
                'Permiss::setdefault',   
                'Permiss::test1', 

                //企业
                'Company::getlist',
            	'Company::companydetail',
            	'Company::getcompanytotal',
            		
                //园区
                'Park::getlist',
            	'Park::getparktotal',
            		
            	//政策
            	'Policy::getpolicytypelist',
            	'Policy::getpolicylist',
            	'Policy::getpolicydetail',
            	'Policy::getpolicytotal',
            		
            	//智库
            	'Tank::gettanklist',
            	'Tank::gettankdetail',
            	'Tank::gettanktotal',
            	
            	//投融
            	'Invest::getinvesteventlist',
            	'Invest::getinvestorganlist',
            		
                 //标签
                'Label::getchaininfo',//云端产业链
                'Label::getchaindetailbyid',
                'Label::getcityinfo',
                'Label::getindustryinfo',
                'Label::getuserorplatformchain',
            	'Label::getcompanylabelinfo',

                //地图
                'Map::getbusinessmap',//招商地图
            	'Map::companynumonchain',//二维产业链子节点的企业数量
            	'Map::compayorparknumonmap',//省市区对应下面的企业或者园区的数量统计
            	'Map::childrencompanynumonchain',//子集产业链子节点的企业数量
            	'Map::getcompanyheat',//企业热力图
            	
            	//新闻
            	'News::getnewslist',
            	'News::getnewsdetail',
            	'News::newsedit',
            	'News::newsdel' ,
            	'News::updatenewsstatus',
            		
            	//舆情
            	'Opinions::getopinionstypelist',
            	'Opinions::getopinionslist',
            	'Opinions::getopinionsdetail',
            		
            	//视频
            	'Videos::getvideoslist',
            	'Videos::getvideosdetail',
            	'Videos::videosedit',
            	'Videos::videosdel' ,
            	'Videos::updatevideosstatus',
            	
            	//本地招商项目
            	'Localprojects::getlocalprojectslist',
            	'Localprojects::getlocalprojectsdetail',
            	'Localprojects::localprojectsedit',
            	'Localprojects::localprojectsdel' ,
            	'Localprojects::updatelocalprojectsstatus',
            	'Localprojects::getlocalprojectstotal',
            		
            	//投资项目
            	'Investment::getinvestmentlist',
            	'Investment::getinvestmenttotal',
            		
            	//项目线索
            	'Businessclue::getbusinesscluelist',
            	'Businessclue::getbusinesscluedetail',
            	'Businessclue::businessclueedit',
            	'Businessclue::businesscluedel' ,
            	'Businessclue::updatebusinesscluestatus',
            		
            	//监控
            	'Monitor::getmonitorlist',
            	'Monitor::monitoraddorcancel',
            	'Monitor::getmonitornewslist',
            		
            	//尽调报告
            	'Applycomreport::applycomreportadd',
            		
            	//产业报告
            	'Report::getreportlist',
            		
            	//平台
            	'Platform::getplatforminfo',
            		
            	//open
            	'Open::getmapjsonbycode',
            	'Open::uploadfife',
            	
            	//我的项目
            	'Projects::getprojectslist',
            	'Projects::getprojectsdetail',
            	'Projects::projectsedit',
            	'Projects::projectsdel' ,
            	'Projects::updateprojectsstatus',
            	
            	//用户
            	'User::getuseinfo',
            	'User::updateuser',
            	
            	//资源要素
            	'Resource::getinvestcost',
            	'Resource::getmap',
                            
            )
        );
       
        if (in_array($resource, $acl[$role])) {
           
            return true;
        }
        return false;
    }
    public function getUrl(){
        $pathinfo = explode('/',$request->pathinfo());
        $controller = !empty($pathinfo[0]) ? str_replace('.html','',$pathinfo[0]) : 'Index';
        $action = !empty($pathinfo[1]) ? str_replace('.html','',$pathinfo[1]) : 'index';
        if(session('admin_success') == null && $controller != 'Login')
        return redirect(url('Login/index'));
        //权限验证
        $app = app('http')->getName();//应用名
        $route = $request->get();
        if(count($route) > 1)
        array_shift($route);
        else
        $route = [];
    }
}
